/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

/**
 *
 * @author User
 */
import entity.Category;
import iface.IManagerDB;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import manager.CategoryManager;
import manager.DisabledCategoryManager;

public class CategoryDB implements IManagerDB<CategoryManager, DisabledCategoryManager> {

    private CallableStatement cstmt;
    private java.sql.Connection conn;
    private ResultSet rs;

    public CategoryDB(Connection conn) {
        this.conn = conn;
    }

    @Override
    public void loadData(CategoryManager t, boolean value) {
        try {
            cstmt = conn.prepareCall("{call sp_GetCategory(?)}");
            cstmt.setBoolean("Status", true);
            rs = cstmt.executeQuery();

            while (rs.next()) {
                int categoryID = rs.getInt("CategoryID");
                String categoryName = rs.getString("CategoryName");
                boolean status = rs.getBoolean("Status");

                Category category = new Category(categoryID, categoryName, status);
                t.add(category);
            }
        } catch (SQLException ex) {
            Logger.getLogger(CategoryDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void insert(Object object) {
        Category category = (Category) object;

        try {
            cstmt = conn.prepareCall("{call sp_InsertCategory(?,?,?)}");
            cstmt.setInt(1, category.getCategoryID());
            cstmt.setString(2, category.getCategoryName());
            cstmt.setBoolean(3, category.isStatus());

            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(CategoryDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void update(Object object) {
        Category category = (Category) object;

        try {
            cstmt = conn.prepareCall("{call sp_UpdateCategory(?,?,?)}");
            cstmt.setInt(1, category.getCategoryID());
            cstmt.setString(2, category.getCategoryName());
            cstmt.setBoolean(3, category.isStatus());

            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(CategoryDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void delete(Object object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}